﻿
using CoreSoft.Frame.Entity.http;
using FERROU8API.Pub;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UFIDA.U8.MomServiceCommon;

namespace FERROU8API.Plugin
{
    /// <summary>
    /// 销售发货退货单
    /// </summary>
    public class SaleBillPlugin
    {
        public string constr = "";
        private string url = "";

        public bool Delete_After(ref MSXML2.IXMLDOMDocument2 domhead, ref MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            try
            {
               
                MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

                //从上下文获取U8Login对象
                U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
                if (login != null)
                    constr = login.UFDataConnstringForNet;

                if (string.IsNullOrEmpty(constr))
                {
                    errMsg = "SaleBillPlugin，Update_After，获取连接串失败";
                    DebugWriteLog.WriteLine("SaleBillPlugin", "Update_After", errMsg);
                    return false;
                }
                string accid = login.get_cAcc_Id();
                MSXML2.IXMLDOMNode tmphead = domhead.selectSingleNode("//z:row");

                int id = Convert.ToInt32(tmphead.attributes.getNamedItem("sbvid").nodeValue);//入库单号             
                Dictionary<string, string> js;

                js = GetCustDic(accid, id, 0);
                if (js == null)
                {
                    return true;
                }

                var result = HttpHelper.PostDataToServerDic(url, js, "application/x-www-form-urlencoded");
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    DebugWriteLog.WriteLine("SaleBillPlugin", "Delete_After", errMsg);
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                DebugWriteLog.WriteLine("SaleBillPlugin", "Delete_After", ex);
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }


        public bool Update_After(ref MSXML2.IXMLDOMDocument2 domhead, ref MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            try
            {
                
                MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

                //从上下文获取U8Login对象
                U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
                if (login != null)
                    constr = login.UFDataConnstringForNet;

                if (string.IsNullOrEmpty(constr))
                {
                    errMsg = "SaleBillPlugin，Update_After，获取连接串失败";
                    DebugWriteLog.WriteLine("SaleBillPlugin", "Update_After", errMsg);
                    return false;
                }
                string accid = login.get_cAcc_Id();
                MSXML2.IXMLDOMNode tmphead = domhead.selectSingleNode("//z:row");

                int dlid = Convert.ToInt32(tmphead.attributes.getNamedItem("sbvid").nodeValue.ObjToString());//发货单id                

                //String js;

                //js = GetCustJs(accid, dlid, 0);
                //if (js == "Nopermission")
                //{
                //    return true;
                //}
                Dictionary<string, string> js;

                js = GetCustDic(accid, dlid, 0);
                if (js == null)
                {
                    return true;
                }

                var result = HttpHelper.PostDataToServerDic(url, js, "application/x-www-form-urlencoded");
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    DebugWriteLog.WriteLine("SaleBillPlugin", "Update_After", errMsg);
                    throw new Exception(errMsg);
                    //return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                DebugWriteLog.WriteLine("SaleBillPlugin", "Update_After", ex);
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }

        private string GetCustJs(string accid, int id, int operation)
        {
            DebugWriteLog.WriteLine("销售发票接口", "ccode", "id=" + id);

            try
            {

               
                UFIDA.U8.DB.Model.SaleBillVouch model = UFIDA.U8.DB.DAL.SaleBillVouch.GetModel(id, constr);
                if (model == null)
                {
                    DebugWriteLog.WriteLine("销售发票接口", "table", " Nopermission");
                   
                }
                if (!string.IsNullOrEmpty(model.cDefine8) && model.cDefine8.Equals("是"))
                {
                    if (string.IsNullOrEmpty(model.cDefine10))
                    {
                        throw new Exception("内部协同订单，最终客户不能为空");
                    }
                    UFIDA.U8.DB.Model.Customer cus = UFIDA.U8.DB.DAL.Customer.GetModel(model.cCusCode, constr);
                    if (cus == null || string.IsNullOrEmpty(cus.cCusDefine1))
                    {
                        throw new Exception("内部协同销售发票接口, 客户档案设置有误，未设置对应orgcode"); 
                    }

                    url = cus.cCusDefine7;
                    if (string.IsNullOrEmpty(url))
                    {
                        throw new Exception("内部协同销售发票接口, wmsconfig表未配置wms接口地址"); 
                    }
                    url = cus.cCusDefine7 + "SaleBillVouch/Create?";
                    ApiRequest apireq = new ApiRequest();

                    apireq.data = model;
                    apireq.orgcode = cus.cCusDefine1;
                    apireq.primarykey = id.ObjToString();
                    apireq.operation = operation;
                    //apireq.csource = accid;

                    DebugWriteLog.WriteLine("销售发票接口", "抛出参数", apireq.ToJson());
                    return apireq.ToJson();
                }
                else
                {
                    return "Nopermission";
                }
              
                
            }
            catch (Exception e)
            {
                DebugWriteLog.WriteLine("发货单退货单接口", "异常", e);
                throw e;
            }
        }

        private Dictionary<string, string> GetCustDic(string accid, int id, int operation)
        {
            DebugWriteLog.WriteLine("销售发票接口", "ccode", "id=" + id);

            try
            {
                    
                UFIDA.U8.DB.Model.SaleBillVouch model = UFIDA.U8.DB.DAL.SaleBillVouch.GetModel(id, constr);
                if (model == null)
                {
                    DebugWriteLog.WriteLine("销售发票接口", "table", " Nopermission");

                }
                if (!string.IsNullOrEmpty(model.cDefine8) && model.cDefine8.Equals("是"))
                {
                    if (string.IsNullOrEmpty(model.cDefine10))
                    {
                        throw new Exception("内部协同订单，最终客户不能为空");
                    }
                    UFIDA.U8.DB.Model.Customer cus = UFIDA.U8.DB.DAL.Customer.GetModel(model.cCusCode, constr);
                    if (cus == null || string.IsNullOrEmpty(cus.cCusDefine1))
                    {
                        throw new Exception("内部协同销售发票接口, 客户档案设置有误，未设置对应orgcode");
                    }

                    url = cus.cCusDefine7;
                    if (string.IsNullOrEmpty(url))
                    {
                        throw new Exception("内部协同销售发票接口, wmsconfig表未配置wms接口地址");
                    }
                    url = cus.cCusDefine7 + "SaleBillVouch/Create?";
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    dic.Add("orgcode", cus.cCusDefine1);
                    dic.Add("operation", operation.ToString());
                    dic.Add("primarykey", model.cSOCode);
                    dic.Add("csource", accid);
                    dic.Add("data", model.ToJson());
                    DebugWriteLog.WriteLine("销售发票接口", "抛出参数", model.ToJson());
                    return dic;
                }
                else
                {
                    return null;
                }


            }
            catch (Exception e)
            {
                DebugWriteLog.WriteLine("发货单退货单接口", "异常", e);
                throw e;
            }
        }

    }
}
